package com.sky.service.impl;

import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.service.DishFlavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishFlavorServiceImpl implements DishFlavorService {

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Override
    public void addFlavorService(List<DishFlavor> flavors) {
        dishFlavorMapper.addDishFlavor(flavors);

    }

    @Override
    public List<DishFlavor> findflavorByDishId(Long id) {

        return dishFlavorMapper.findflavorByDishId(id);

    }

    @Override
    public void updateFlavor(List<DishFlavor> flavors) {
        // 删除所有已经关联的口味
        if(!flavors.isEmpty()){
            dishFlavorMapper.delAllFlavors(flavors.get(0).getDishId());

            // 重新关联口味
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public void delAllFlavorsById(List<DishFlavor> dishFlavors) {
        dishFlavors.forEach(dishFlavor -> {
            dishFlavorMapper.delAllFlavors(dishFlavor.getDishId());
        });
    }
}
